<template>
  <!-- 邀请有你 -->
  <div class="app-container">
    <div class="btn">
      <el-button type="primary" @click="toRoutes('addActive')">创建活动</el-button>
    </div>
    <div class="listDiv" v-loading="tableLoading">
      <el-table ref="tableData" :data="tableData" style="width: 100%">
        <!--  -->
        <el-table-column
          fixed
          type="index"
          label="序号"
          :index="table_index"
          width="50"
          align="center"
        />
        <el-table-column label="活动名称" prop="activity_name" align="center" />
        <el-table-column label="活动时间" prop="hospital_name" align="center">
          <template slot-scope="scope">
            <span>{{scope.row.start_time}}~{{scope.row.end_time}}</span>
          </template>
        </el-table-column>
        <el-table-column label="备注" prop="remark" align="center" />
        <el-table-column label="上线开关" prop="status" align="center">
          <!-- v-if="scope.row.is_complete == 1" :inactive-value="2"-->
          <template slot-scope="scope">
            <div @click="switchChange(scope.row.id,scope.row.status,scope.row.is_complete)">
              <el-switch
                :disabled="scope.row.disabled"
                v-model="scope.row.status"
                :active-value="1"
                :inactive-value="2"
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center" width="230px">
          <template slot-scope="scope">
            <el-button
              size="small"
              type="text"
              v-if="scope.row"
              @click="toRoutes('editActive',scope.row.id)"
            >编辑</el-button>
            <el-button size="small" type="text" @click="handleRemark(scope.row.id)">备注</el-button>
            <el-button
              size="small"
              type="text"
              v-if="scope.row"
              @click="toRoutes('invitedRecord',scope.row.id)"
            >邀请记录</el-button>
            <el-button
              size="small"
              type="text"
              v-if="scope.row"
              @click="toRoutes('prizeRecord',scope.row.id)"
            >奖品记录</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagesCon">
        <div>
          <el-pagination
            :current-page="form1.page"
            :page-sizes="page_sizes"
            :page-size="form1.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page_totals"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
    <!-- 备注弹窗 -->
    <el-dialog title="填写备注" :visible.sync="dialogFormVisible" width="568px" @close="close" center>
      <el-form ref="form" :model="form" :rules="rules" label-position="top">
        <el-form-item label="请填写备注" prop="remark">
          <el-input
            type="textarea"
            placeholder="请输入内容"
            v-model="form.remark"
            size="small"
            :rows="4"
          ></el-input>
        </el-form-item>
        <div class="mt_20">
          <el-button type="primary" @click="confirm('form')">确定</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getList, getSwitch, getRemark } from "@/api/invitationMange";
export default {
  data() {
    return {
      tableData: [{}],
      tableLoading: true,
      dialogFormVisible: false,
      form: {
        id: "",
        remark: ""
      },
      form1: {
        page: 1,
        size: 10
      },
      // disabled: false,
      // 分页
      page_sizes: [10, 30, 50, 100], // 每页显示个数
      page_totals: 0, // 总条目数,
      rules: {
        remark: [
          {
            required: true,
            message: "请填写备注",
            trigger: "blur"
          }
        ]
      }
    };
  },

  created() {
    this.getList(this.form1);
  },
  methods: {
    // 序列号
    table_index(index) {
      return (this.form1.page - 1) * this.form1.size + index + 1;
    },
    toRoutes(e, id) {
      const route = { name: e, query: { id: id } };
      this.$router.push(route);
    },
    // 获取列表
    getList(opt) {
      getList(opt)
        .then(res => {
          if (res.data.code == 200) {
            console.log("res", res.data);
            if (!res.data.data) {
              this.tableData = [];
              return;
            }
            this.tableData = res.data.data;
             this.tableData.forEach(item => {
                if (item.is_complete == 0) {
                  item["disabled"] = true;
                } else {
                  item["disabled"] = false;
                }
              });
            this.page_totals = res.data.meta.total;
          } else {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: "warning"
            });
          }
          this.tableLoading = false;
        })
        .catch(err => {
          this.tableLoading = false;
          // this.errorStatus(err.msg);
          console.log(err)
        });
    },
    // switch开关切换
    switchChange(id, opt, is_complete) {
      let data = { id, opt };
      console.log("opt", opt);
      // console.log("99", is_complete);
      if (is_complete == 0) {
        // console.log(888);
        this.$message({
          showClose: true,
          message:
            "请将用户基础信息配置、邀请人、被邀请人奖品配置及分享等活动规则完善后才可上线哦~~",
          type: "warning"
        });
        // opt = 2;
        return false;
      } else if (is_complete == 1) {
        getSwitch(data)
          .then(res => {
            if (res.data.code == 200) {
              this.$message({
                showClose: true,
                message: "修改成功",
                type: "success"
              });
            } else {
              this.$message({
                showClose: true,
                message: res.data.msg,
                type: "warning"
              });
            }
          })
          .catch(err => {
            this.errorStatus(err.msg);
          });
      }
    },
    // 弹窗备注接口
    handleRemark(id) {
      this.dialogFormVisible = true;
      this.form.id = id;
    },
    // 弹窗确认
    confirm(form) {
      let data = {
        id: this.form.id,
        remark: this.form.remark
      };
      this.$refs[form].validate(valid => {
        if (valid) {
          getRemark(data)
            .then(res => {
              if (res.data.code == 200) {
                this.$message({
                  showClose: true,
                  message: "填写成功",
                  type: "success"
                });
                this.dialogFormVisible = false;
                this.getList();
              } else {
                this.$message({
                  showClose: true,
                  message: res.data.msg,
                  type: "warning"
                });
              }
            })
            .catch(err => {
              this.errorStatus(err.msg);
            });
        } else {
          this.$message({
            message: "有必填项没填，不能提交咯",
            type: "warning"
          });
          return false;
        }
      });
    },
    // 备注弹窗关闭
    close() {
      this.dialogFormVisible = false;
      this.form = { id: "", remark: "" };
    },
    // 改变页面条数
    handleSizeChange(val) {
      this.form1.size = val;
      this.getList(this.form1);
    },
    // 选择页数
    handleCurrentChange(val) {
      this.form1.page = val;
      this.getList(this.form1);
    },
    // // 弹窗组件
    eleMsg(txt, status) {
      this.$message({
        showClose: true,
        message: txt,
        type: status
      });
    },

    // 失败状态弹窗
    errorStatus(txt) {
      this.eleMsg(txt, "error");
    }
  }
};
</script>
<style scoped>
.btn {
  margin-bottom: 20px;
}
.pagesCon {
  margin-top: 20px;
  text-align: right;
}
</style>